// 引入pinia
import { defineStore } from 'pinia'
// 引入后台api
import { getProducts, IProduct } from '@/api/shop'
export const useProductsStore = defineStore('products', {
  state: () => {
    return {
      // 商品数据
      goods: [] as IProduct[]
    }
  },
  getters: {

  },
  actions: {
    // 获取商品列表
    async getGoodsList() {
      const res = await getProducts()
      this.goods = res
    },
    // 减库存
    decrementProduct(product: IProduct) {
      const itemGoods = this.goods.find(item => item.id === product.id )
      if (itemGoods && itemGoods.inventory>0) itemGoods.inventory--
    }
  },
  persist: {
    enabled: true,
    /* strategies: [
      {
        key: 'products',
        storage: localStorage
      }
    ] */
  }
})